package m.html.util;

import m.common.model.util.QueryPage;
import m.html.element.Button;
import m.html.element.Element;
import m.html.element.Icon;
import m.html.element.Text;
import m.html.element.attr.ButtonType;
import m.html.element.other.DisplayItem;
import m.html.element.other.IElement;
import m.html.list.ListSearch;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class VueListUtil {

    public static <T extends IVueListColumn> Element toViewColumnElement(List<T> cols, Map<String,String> params){
        Element listElement=Element.tag("a-list").A("size","small").A("style","max-height:400px;overflow-y:auto;");
        listElement.E(
            Element.tag("a-list-item").A("v-for","(item,idx) in columnsSetting")
            .E(
                Element.tag("a-list-item-meta")
                .E(
                    Element.tag("template").A("#avatar")
                    .E(Element.tag("a-switch").A("v-model:checked","columnsSetting[idx].show").A("size","small").A("checkedValue","Y").A("unCheckedValue","N"))
                ),
                Element.tag("div").A("style", "flex:10;")
                .E(new Text("{{item.title}}"))
            )
            .E(
                Element.tag("template").A("#actions")
                .E(new Icon("icon-less").A("v-if","item.sort=='Y'&&columnsSetting[idx-1]&&columnsSetting[idx-1].sort=='Y'").A("@click","setColumnsSort(idx,-1)"))
                .E(new Icon("icon-moreunfold").A("v-if","item.sort=='Y'&&columnsSetting[idx+1]&&columnsSetting[idx+1].sort=='Y'").A("@click","setColumnsSort(idx,1)"))
            )
        );
        Element viewColElement=Element.tag("div").A("style", "width:350px;margin:-10px -10px 0;");
        viewColElement.E(listElement);
        viewColElement.E(
            Element.tag("div").A("style","padding-top:8px;text-align:center;border-top: 1px solid #8884;")
                .E(new Button(ButtonType.PRIMARY).A("size","small").A("style", "width:130px;").E(new Text("确定")).setExtend(" @click=\"queryData(1,page.num,{save_columns:getColumnsParam()});\" "))
        );
        return viewColElement;
    }

    public static Element toSearchPanel(List<ListSearch> searchMap,int defaultSearchNum){
        boolean hasSearch=false;
        List<IElement> searchItems= new ArrayList<>();
        int n=0;
        StringBuilder fs= new StringBuilder();
        for(ListSearch ls : searchMap) {
            String tx=ls.toString();
            if(!StringUtil.isSpace(tx)) {
                n++;
                hasSearch=true;
                fs.append(",'").append(ls.getField()).append("'");
                Element label=StringUtil.notSpace(ls.getTitleDescription())?
                    Element.tag("a-tooltip").E(Element.tag("template").A("#title").E(ls.getTitleDescription())).E(ls.getTitle())
                    :new Text(ls.getTitle());
                Element e=Element.tag("a-descriptions-item")
                    .E(Element.tag("template").A("#label").E(label))
                    .A(":span",String.valueOf(ls.getSpan()))
                    .E(new Text(tx));
                if(n>defaultSearchNum) e.A("v-if","showSearch");
                searchItems.add(e);
            }
        }
        if(!hasSearch) return null;
        Element e=Element.tag("a-card").A(":hoverable","true").A("bodyStyle","padding:0;")
        .E(
            Element.tag("a-descriptions").A(":column","{xs:1,sm:1,md:2,lg:2,xl:3,xxl:4,xxxl:5}").A("class","index-normal")
            .E(searchItems)
            .E(
                Element.tag("a-descriptions-item").A("class","text-right")
                .E(Element.tag("a-space").E(
                    new Button(ButtonType.PRIMARY).E(new Text("查询")).setExtend(" @click=\"queryData();\""),
                    new Button(ButtonType.GHOST).E(new Text("重置")).setExtend(" @click=\"resetSearchField(["+fs.substring(1)+"]);queryData();\""),
                    n<=defaultSearchNum?null:new Button(ButtonType.LINK).setExtend(" @click=\"showSearch=!showSearch;autoSetBodyHeight();\"")
                        .E(
                            new Text("{{showSearch?'收起':'展开'}}"),
                            new Icon("").setSize(16).A(":name","showSearch?'icon-less':'icon-moreunfold'")
                        )
                ))
            )
        );
        return Element.tag("div").A("style","padding:0 15px 15px;")
                .E(e);
    }
    public static Element toText(String dateFormat,String numberFormat,String dictType,List<DisplayItem> disItems,boolean copy){
        if(!StringUtil.isSpace(dateFormat)) {//含日期格式
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:formatter('date',text,'"+dateFormat+"')}:false")
                    .E(new Text("{{formatter('date',text,'"+dateFormat+"')}}"));
            else
                return new Text("{{formatter('date',text,'"+dateFormat+"')}}");
        }else if(!StringUtil.isSpace(numberFormat)) {//含数字格式
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:formatter('number',text,'"+numberFormat+"')}:false")
                    .E(new Text("{{formatter('number',text,'"+numberFormat+"')}}"));
            else
                return new Text("{{formatter('number',text,'"+numberFormat+"')}}");
        }else if(!StringUtil.isSpace(dictType)){//有字典项
            if(!dictType.startsWith("'")) dictType="'"+dictType+"'";
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:dict("+dictType+",text)}:false")
                    .E(new Text("{{dict("+dictType+",text)}}"));
            else
                return Element.tag("span").A(":title","dict("+dictType+",text)").E(new Text("{{dict("+dictType+",text)}}"));
        }else if(!disItems.isEmpty()){//有显示选项
            Element tmp=Element.tag("span");
            for (DisplayItem item : disItems) {
                tmp.E(Element.tag("span").A("v-if", "outTxt(text)=='" + item.getValue() + "'").E(new Text(item.getTitle())));
            }
            return tmp;
        }else {
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:text}:false")
                    .E(Element.tag("span").A(":title","text").E(new Text("{{outTxt(text)}}")));
            else
                return Element.tag("span").A(":title","text").E(new Text("{{outTxt(text)}}"));
        }
    }
    public static String getPageSizes(int pageSize){
        String ps="'10','20','50','100','200','500'";
        if(!ps.contains("'" + pageSize + "'")){
            return "'"+pageSize+"',"+ps;
        }else{
            return ps;
        }
    }
    public static String getPageNum(QueryPage page,int pageSize){
        if(null==page) return "9999999999";
        else return String.valueOf(pageSize);
    }
}
